<article id="wikiArticle">
<div></div>
<p><strong><code>Math.tanh()</code></strong> 函数将会返回一个数的双曲正切函数值，计算如下:</p>
<p><math display="block"><semantics><mrow><mo lspace="0em" rspace="0em">tanh</mo><mi>x</mi><mo>=</mo><mfrac><mrow><mo lspace="0em" rspace="0em">sinh</mo><mi>x</mi></mrow><mrow><mo lspace="0em" rspace="0em">cosh</mo><mi>x</mi></mrow></mfrac><mo>=</mo><mfrac><mrow><msup><mi>e</mi><mi>x</mi></msup><mo>-</mo><msup><mi>e</mi><mrow><mo>-</mo><mi>x</mi></mrow></msup></mrow><mrow><msup><mi>e</mi><mi>x</mi></msup><mo>+</mo><msup><mi>e</mi><mrow><mo>-</mo><mi>x</mi></mrow></msup></mrow></mfrac><mo>=</mo><mfrac><mrow><msup><mi>e</mi><mrow><mn>2</mn><mi>x</mi></mrow></msup><mo>-</mo><mn>1</mn></mrow><mrow><msup><mi>e</mi><mrow><mn>2</mn><mi>x</mi></mrow></msup><mo>+</mo><mn>1</mn></mrow></mfrac></mrow><annotation encoding="TeX">\tanh x = \frac{\sinh x}{\cosh x} = \frac {e^x - e^{-x}} {e^x + e^{-x}} = \frac{e^{2x} - 1}{e^{2x}+1}</annotation></semantics></math></p>
<h2 id="语法">语法</h2>
<pre><code class="language-javascript"><code>Math.tanh(<var>x</var>)</code></code></pre>
<h3 id="参数">?参数</h3>
<dl>
<dt><code>x</code></dt>
<dd>待计算的数字</dd>
</dl>
<h3 id="返回值">返回值</h3>
<p>所给数字的双曲正切值。</p>
<h2 id="描述">描述</h2>
<p><code>因为tanh()是Math的一个静态方法, 所以应该直接通过Math.tanh()来使用,而不是由用户先创建出Math对象再调用该方法。(Math不是一个构造器)。</code></p>
<h2 id="示例">示例</h2>
<h3 id="使用_Math.tanh()">使用 <code>Math.tanh()</code></h3>
<pre><code  class="language-javascript">Math.tanh(0);        // 0
Math.tanh(Infinity); // 1
Math.tanh(1);        // 0.7615941559557649
</code></pre>
<h2 id="多种实现方式">多种实现方式</h2>
<p>tanh()可以通过<a href="Reference/Global_Objects/Math/exp" title="Math.exp() 函数返回 ex，x 表示参数，e 是欧拉常数（Euler's constant），自然对数的底数。"><code>Math.exp()</code></a>函数来构拟:</p>
<pre><code  class="language-javascript">Math.tanh = Math.tanh || function(x) {
  if (x === Infinity) {
    return 1;
  } else if (x === -Infinity) {
    return -1;
  } else {
    return (Math.exp(x) - Math.exp(-x)) / (Math.exp(x) + Math.exp(-x));
  }
}
</code></pre>
<p>或者只调用一次<a href="Reference/Global_Objects/Math/exp" title="Math.exp() 函数返回 ex，x 表示参数，e 是欧拉常数（Euler's constant），自然对数的底数。"><code>Math.exp()</code></a>:</p>
<pre><code  class="language-javascript">Math.tanh = Math.tanh || function(x) {
  if (x === Infinity) {
    return 1;
  } else if (x === -Infinity) {
    return -1;
  } else {
    var y = Math.exp(2 * x);
    return (y - 1) / (y + 1);
  }
}
</code></pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">规范</th>
<th scope="col">状态</th>
<th scope="col">?注释</th>
</tr>
<tr>
<td><a class="external" href="https://www.ecma-international.org/ecma-262/6.0/#sec-math.tanh" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">Math.tanh</small></a></td>
<td><span class="spec-Standard">Standard</span></td>
<td>初始定义</td>
</tr>
<tr>
<td><a class="external" href="https://tc39.github.io/ecma262/#sec-math.tanh" hreflang="en" lang="en" rel="noopener">ECMAScript Latest Draft (ECMA-262)<br/><small lang="zh-CN">Math.tanh</small></a></td>
<td><span class="spec-Draft">Draft</span></td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<div><div class="blockIndicator warning"><strong><a class="external" href="https://github.com/mdn/browser-compat-data" rel="noopener">We're converting our compatibility data into a machine-readable JSON format</a></strong>.
            This compatibility table still uses the old format,
            because we haven't yet converted the data it contains.
            <strong><a class="new" href="/zh-CN/docs/MDN/Contribute/Structures/Compatibility_tables" rel="nofollow">Find out how you can help!</a></strong></div>
<div class="htab">
<a id="AutoCompatibilityTable" name="AutoCompatibilityTable"></a>
<ul>
<li class="selected"><a>Desktop</a></li>
<li><a>Mobile</a></li>
</ul>
</div></div>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>?特征</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>基础支持</td>
<td>38</td>
<td><a href="/en-US/Firefox/Releases/25" title="Released on 2013-10-29.">25</a> (25)</td>
<td><span style="color: #f00;">未实现</span></td>
<td>25</td>
<td>7.1</td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>特征</th>
<th>Android</th>
<th>Chrome for Android</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>基础支持</td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
<td>25.0 (25)</td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
<td>8</td>
</tr>
</tbody>
</table>
</div>
<h2 id="参见">参见</h2>
<ul>
<li><a class="new" href="Reference/Global_Objects/Math/acosh" rel="nofollow" title="此页面仍未被本地化, 期待您的翻译!"><code>Math.acosh()</code></a></li>
<li><a href="Reference/Global_Objects/Math/asinh" title="Math.asinh() 函数返回给定数字的反双曲正弦值, 即："><code>Math.asinh()</code></a></li>
<li><a href="Reference/Global_Objects/Math/atanh" title="Math.atanh() 函数返回一个数值反双曲正切值, 即："><code>Math.atanh()</code></a></li>
<li><a href="Reference/Global_Objects/Math/cosh" title="Math.cosh() 函数返回数值的双曲余弦函数, 可用 constant e 表示:"><code>Math.cosh()</code></a></li>
<li><a href="Reference/Global_Objects/Math/sinh" title="Math.sinh() 函数返回一个数字(单位为角度)的双曲正弦值."><code>Math.sinh()</code></a></li>
</ul>
</article>